

<title>文章管理</title>
  
<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">控制台</a>
    <a><cite>文章管理</cite></a>
  </div>
</div>

<div class="layui-fluid">   
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="back-article-list-search">
      <div class="layui-form-item">
        <div class="layui-inline">
          <label class="layui-form-label">标题</label>
          <div class="layui-input-block">
            <input type="text" name="title" placeholder="请输入标题" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">类型</label>
          <div class="layui-input-block">
            <select name="type_id" id="back-article-list-search-type-select">
              <option value="">全部</option>
            </select>
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">状态</label>
          <div class="layui-input-block">
            <select name="status">
              <option value="">全部</option>
              <option value="1">启用</option>
              <option value="0">禁用</option>
            </select>
          </div>
        </div>
        <div class="layui-inline">
          <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="back-article-list-search-submit">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
          </button>
        </div>
      </div>
    </div>
    
    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-admin" data-type="add">添加</button>
      </div>
      
      <table id="back-article-list" lay-filter="back-article-list"></table>
      <script type="text/html" id="comment_status_switch">
        <input type="checkbox" name="" lay-skin="switch" lay-text="开启|禁用" lay-filter="comment_status_switch" value="{{d.id}}" 
        {{ d.comment_status == 1 ? 'checked' : '' }}>
      </script>
      <script type="text/html" id="status_switch">
        <input type="checkbox" name="" lay-skin="switch" lay-text="显示|隐藏" lay-filter="status_switch" value="{{d.id}}" 
        {{ d.status == 1 ? 'checked' : '' }}>
      </script>
      <script type="text/html" id="back-article-list-operation">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
      </script>
    </div>
  </div>
</div>

<script>
layui.use('article', layui.factory('article')).use(['admin', 'article', 'table','upload','layedit'], function(){
  var $ = layui.$
  ,admin = layui.admin
  ,view = layui.view
  ,table = layui.table
  ,form = layui.form
  ,setter = layui.setter
  ,upload = layui.upload
  ,layedit = layui.layedit;
  
  // 类型下拉框
  admin.req({
    url: setter.api_back_article_type_list
    ,done: function(res){
      var select = $('#back-article-list-search-type-select');
      var code = [];
      $.each(res.data.list, function (index, item) {
        if(item.pid == 0){
          code[item.id] = "<optgroup label=\""+item.name+"\" >";
        }else{
          code[item.pid] = code[item.pid] + "<option value=\""+item.id+"\">"+item.name+"</option>";
        }
      });
      $.each(code, function (index, item) {
        if(item){
          select.append(item + "</optgroup>");
        }
      });
      form.render(null, 'back-article-list-search');
    }

  });

  //监听搜索
  form.on('submit(back-article-list-search-submit)', function(data){
    var field = data.field;
    //执行重载
    table.reload('back-article-list', {
      where: field
    });
  });

  //监听 状态switch 
  form.on('switch(status_switch)', function(data){
    if(data.elem.checked == true){
      admin.req({
        url:setter.api_back_article_update,
        data:{'id':data.value,'status':1},
        done:function(res){
          layer.msg(res.msg);
          table.reload('back-article-list'); //重载表格
        }
        ,faile:function(res) {
          table.reload('back-article-list'); //重载表格
        }
      });
    }else{
      admin.req({
        url:setter.api_back_article_update,
        data:{'id':data.value,'status':0},
        done:function(res){
          layer.msg(res.msg);
          table.reload('back-article-list'); //重载表格
        }
        ,faile:function(res) {
          table.reload('back-article-list'); //重载表格
        }
      });
    }
  });

  //监听 评论switch 
  form.on('switch(comment_status_switch)', function(data){
    if(data.elem.checked == true){
      admin.req({
        url:setter.api_back_article_update,
        data:{'id':data.value,'comment_status':1},
        done:function(res){
          layer.msg(res.msg);
          table.reload('back-article-list'); //重载表格
        }
        ,faile:function(res) {
          table.reload('back-article-list'); //重载表格
        }
      });
    }else{
      admin.req({
        url:setter.api_back_article_update,
        data:{'id':data.value,'comment_status':0},
        done:function(res){
          layer.msg(res.msg);
          table.reload('back-article-list'); //重载表格
        }
        ,faile:function(res) {
          table.reload('back-article-list'); //重载表格
        }
      });
    }
  });

  //事件
  var active = {
    add: function(){
      admin.popup({
        title: '创建文章'
        ,area: admin.screen() < 2 ? ['100%','600px'] : ['710px','880px']
        ,maxmin: true
        ,id: 'back-article-add-popup'
        ,success: function(layero, index){
          view(this.id).render('article/form').done(function(){
            // 富文本编辑器
            var layeditInst = layedit.build('myEditor');
            // 封面上传
            var uploadInst = upload.render({
              elem: '#upload-cover'
              ,url: setter.api_back_image_upload
              ,before: function(obj){
                obj.preview(function(index, file, result){
                  $('#upload-cover-img').attr('src', result); //图片链接（base64）
                });
              }
              ,done: function(res){
                if(res.code > 0){
                  return layer.msg('上传失败');
                }else{
                  $('#upload-cover-input').val(res.data.src);
                }
              }
              ,error: function(){
                var text = $('#upload-cover-text');
                text.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini reload">重试</a>');
                text.find('.reload').on('click', function(){
                  uploadInst.upload();
                });
              }
            });
            // 类型下拉框
            admin.req({
              url: setter.api_back_article_type_list
              ,done: function(res){
                var select = $('#back-article-form-type-select');
                var code = [];
                $.each(res.data.list, function (index, item) {
                  if(item.pid == 0){
                    code[item.id] = "<optgroup label=\""+item.name+"\" >";
                  }else{
                    code[item.pid] = code[item.pid] + "<option value=\""+item.id+"\">"+item.name+"</option>";
                  }
                });
                $.each(code, function (index, item) {
                  if(item){
                    select.append(item + "</optgroup>");
                  }
                });
                form.render(null, 'back-article-form');
              }
            });
            
            //监听提交
            form.on('submit(back-article-form-submit)', function(data){
              var field = data.field; //获取提交的字段
              field.status = field.status?1:0;
              field.comment_status = field.comment_status?1:0;
              field.content = layedit.getContent(layeditInst);
              admin.req({
                url: setter.api_back_article_create
                ,data: field
                ,done: function(res){
                  layer.msg(res.msg);
                  layer.close(index); //执行关闭 
                  table.reload('back-article-list'); //重载表格
                }
              });
            });
            $('#layer-close').on('click', function(event) {
              layer.close(index); //执行关闭 
              /* Act on the event */
            });
          });
        }
      });

    }
  }  
  $('.layui-btn.layuiadmin-btn-admin').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
});
</script>